package com.cloud.wms.ui.returned.relay;


import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cloud.widget.ScanText;
import com.cloud.widget.expand.ExpandLayout;
import com.cloud.wms.R;
import com.cloud.wms.base.BaseBarListActivity;
import com.cloud.wms.ui.main.entity.CommodityEntity;
import com.cloud.wms.ui.returned.contact.RelayContract;
import com.cloud.wms.ui.returned.entity.RelayBoxEntity;
import com.cloud.wms.ui.returned.presenter.RelayPresenter;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @Author:Chen
 * @Date: 2021-03-01 16:51
 * @Description:
 */
public class RelayBoxActivity extends BaseBarListActivity<RelayBoxEntity, RelayPresenter> implements RelayContract.View {

    @BindView(R.id.scan_text)
    ScanText scanText;
    @BindView(R.id.ll_btn)
    LinearLayout llBtn;
    @BindView(R.id.btn_reset)
    TextView btnReset;
    @BindView(R.id.btn_confirm_relay)
    TextView btnConfirmRelay;

    @Override
    protected String barTitle() {
        return "退货交接";
    }

    @Override
    protected int getResId() {
        return R.layout.activity_returned_relay;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        initScanText(scanText);
        rvList.setVisibility(View.GONE);
        llBtn.setVisibility(View.GONE);
    }

    @Override
    protected int getItemResId() {
        return R.layout.item_returned_relay_box;
    }

    @Override
    protected void bindItemData(BaseViewHolder helper, RelayBoxEntity item) {
        helper.setText(R.id.tv_box_code, item.getContainerCode())
                .setText(R.id.tv_scan_status, item.isScan() ? "已扫描" : "未扫描")
                .setBackgroundResource(R.id.tv_scan_status, item.isScan() ? R.drawable.bg_green : R.drawable.bg_red);
        ImageView ivArrow = helper.getView(R.id.iv_arrow);
        ExpandLayout expand = helper.getView(R.id.expand_layout);
        expand.setOnExpansionUpdateListener((expansionFraction, state) -> {
            ivArrow.setRotation(expansionFraction * 180);
        });
        if (mAdapter.getData().size() == 1) {
            helper.setGone(R.id.ll_title_content, true);
            expand.expand();
        }
        helper.getView(R.id.ll_title_content).setOnClickListener(v -> expand.toggle(true));

        RecyclerView rvChildList = helper.getView(R.id.rv_child_list);
        rvChildList.setLayoutManager(new LinearLayoutManager(RelayBoxActivity.this));
        BaseQuickAdapter mChildAdapter = new BaseQuickAdapter<CommodityEntity, BaseViewHolder>(R.layout.item_returned_relay_commodity, item.getCommodityList()) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, CommodityEntity item) {
                helper.setText(R.id.tv_commodity_name, item.getCommodityName())
                        .setText(R.id.tv_commodity_code, item.getBarCode())
                        .setText(R.id.tv_commodity_type, item.getStockTypeName())
                        .setText(R.id.tv_commodity_amount, item.getAmount() + "")
                        .setText(R.id.tv_produce_date, item.getProductionDate())
                        .setText(R.id.tv_deadline_date, item.getExpirationDate())
                        .setText(R.id.tv_in_date, item.getInDate());
            }
        };

        rvChildList.setAdapter(mChildAdapter);
    }

    @Override
    protected void onScanHandle(String result, int resultCode) {
        super.onScanHandle(result, resultCode);
        scanText.addHistory(result);
        if (mAdapter.getData().size() > 0) {
            for (int i = 0; i < mAdapter.getData().size(); i++) {
                RelayBoxEntity boxEntity = mAdapter.getData().get(i);
                if (!boxEntity.isScan() && result.equalsIgnoreCase(boxEntity.getContainerCode())) {
                    boxEntity.setScan(true);
                    mAdapter.notifyDataSetChanged();
                    success("容器扫描成功！");
                    btnConfirmRelay.setEnabled(isScanComplete());
                    return;
                }
            }
            info("扫描容器不在当前关联交接范围之内！");
        } else {
            mPresenter.getBoxCommodityInfo(result);
        }
    }

    private boolean isScanComplete() {
        boolean isScanComplete = true;
        for (int i = 0; i < mAdapter.getData().size(); i++) {
            RelayBoxEntity boxEntity = mAdapter.getData().get(i);
            if (!boxEntity.isScan()) {
                isScanComplete = false;
            }
        }
        return isScanComplete;
    }

    @Override
    protected void setupComponent() {
        getActivityComponent().inject(this);
    }

    @Override
    public void onSuccess(Message message) {
        super.onSuccess(message);
        switch (message.what) {
            case RelayPresenter.GET_BOX_COMMODITY:
                rvList.setVisibility(View.VISIBLE);
                llBtn.setVisibility(View.VISIBLE);
                mAdapter.setNewInstance(((ArrayList<RelayBoxEntity>) message.obj));
                boolean complete = isScanComplete();
                if (complete) {
                    info("请确认商品无误，交接确认！");
                } else {
                    info("请扫描列表中标记为未扫描的容器！");
                }

                btnConfirmRelay.setEnabled(complete);
                break;
            case RelayPresenter.CONFIRM_RETURN_RELAY:
                success("交接提交成功！");
                this.finish();
                break;
        }
    }

    @OnClick({R.id.btn_reset, R.id.btn_confirm_relay})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_reset:
                rvList.setVisibility(View.GONE);
                mAdapter.getData().clear();
                mAdapter.notifyDataSetChanged();
                llBtn.setVisibility(View.GONE);
                scanText.setEnabled(true);
                scanText.requestFocus();
                success("重置成功，请扫描容器！");
                break;
            case R.id.btn_confirm_relay:
                mPresenter.confirmRelay(mAdapter.getData());
                break;
        }
    }
}
